package com.scs.application.modules.msg.provider;

import com.scs.application.core.msg.domain.RawMessage;
import com.scs.application.core.msg.domain.SceneMessage;
import com.scs.application.core.msg.domain.WechatMessage;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.msg.entity.Template;
import com.scs.application.modules.upms.vo.UserVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 微信消息处理
 */

@Component
@AllArgsConstructor
public class BuildWechatMessageProvider implements BuildMessageProvider<WechatMessage> {
    @Override
    public List<RawMessage> messagesBuild(List<UserVO> userInfoVOList, Template template, SceneMessage msg) {

        List<RawMessage> messageList = new ArrayList<>();
        userInfoVOList.stream().forEach((user) -> {
            WechatMessage wechatMessage = new WechatMessage();
            wechatMessage.setTemplateCode(StringUtils.stripHtml(template.getContent()).trim())
                    .setParams(getSuitableParams(msg, wechatMessage.getMessageType()))
                    .setData(msg.getData())
                    .setTemplateId(msg.getTemplateId())
                    .setToUrl(msg.getToUrl())
                    .setLoginKey(user.getLoginKey());
            messageList.add(wechatMessage);
        });
        return messageList;
    }

    @Override
    public boolean support(Class<? extends RawMessage> clz) {
        return (WechatMessage.class.isAssignableFrom(clz));
    }

}
